package com.wtx.leetcode;

/**
 * @description: leetcode 面试题03.04 化栈为队 pass 执行用时17ms，内存消耗 37.1mb
 * @author: wtx
 * @createDate: 2020/5/24
 */

import java.util.Stack;

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */
public class MyQueue1 {

    private Stack<Integer> stack;
    private Stack<Integer> helpStack;

    /** Initialize your data structure here. */
    public MyQueue1() {

        this.stack = new Stack<>();
        this.helpStack = new Stack<>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        stack.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        while(!stack.isEmpty())
            helpStack.push(stack.pop());

        Integer ret = helpStack.pop();
        while (!helpStack.isEmpty())
            stack.push(helpStack.pop());
        return ret;
    }

    /** Get the front element. */
    public int peek() {

        while (!stack.isEmpty())
            helpStack.push(stack.pop());
        Integer peek = helpStack.peek();
        while (!helpStack.isEmpty())
            stack.push(helpStack.pop());
        return peek;
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack.isEmpty();
    }
}
